import SoundManager from "../manager/SoundManager";
import Define from "./Define";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class ButtonEffect extends cc.Component {

    private clickCallBack:Function;
    private isClickEffect:boolean = true;
    private isDown:boolean = false;
    start () {
        if(cc.sys.platform === cc.sys.WECHAT_GAME){
            this.node.on(cc.Node.EventType.TOUCH_START, (e: cc.Event.EventTouch) => this.mouseDown(e))
            this.node.on(cc.Node.EventType.TOUCH_END, (e: cc.Event.EventTouch) => this.mouseEnd(e))
            this.node.on(cc.Node.EventType.TOUCH_CANCEL, (e: cc.Event.EventTouch) => this.mouseEnd(e))
        }else {
            this.node.on(cc.Node.EventType.MOUSE_DOWN, (e: cc.Event.EventTouch) => this.mouseDown(e))
            this.node.on(cc.Node.EventType.MOUSE_UP, (e: cc.Event.EventTouch) => this.mouseEnd(e))
            this.node.on(cc.Node.EventType.MOUSE_LEAVE, (e: cc.Event.EventTouch) => this.mouseEnd(e))
        }
    }

    mouseDown (event:cc.Event.EventTouch){
        this.isDown = true
        if(this.isClickEffect){
           // SoundManager.palySoundById(Define.soundButton)
            this.node.runAction(cc.scaleTo(0.1,0.9));
        }
    }

    mouseEnd (event:cc.Event.EventTouch){
        if(this.isDown){
            this.isDown = false;
            if(this.clickCallBack != null){
                this.clickCallBack(this.node.name);
            }
            if(this.isClickEffect){
                this.node.stopAllActions();
                this.node.scale = 1
            }
        }
    }
    
    setClickCallBack(callBack:Function,isClickEffect:boolean = false){
        this.clickCallBack = callBack;
        this.isClickEffect = isClickEffect;
    }
}
